"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.HelpCommand = void 0;
// import { Input } from './command.input';
const child_process_1 = require("child_process");
class HelpCommand {
    load(program) {
        program
            .command('help [command]')
            .description('usage of a specific command')
            .action((cmdName = '', commandOptions) => __awaiter(this, void 0, void 0, function* () {
            if (!cmdName) {
                program.outputHelp();
                return;
            }
            // program.outputHelp()
            yield new Promise((resolve, reject) => {
                const options = {
                    stdio: 'pipe',
                    shell: true,
                };
                // cpaas-cli ${cmdName} --help
                const child = child_process_1.spawn(`node`, [require.resolve('../bin/cpaas-cli'), cmdName, '--help'], options);
                child.stdout.pipe(process.stdout);
                if (child.stderr) {
                    child.stderr.pipe(process.stderr);
                }
                child.on('close', code => {
                    if (code === 0) {
                        resolve(code);
                    }
                    else {
                        reject(code);
                    }
                });
                resolve();
            });
        }));
    }
}
exports.HelpCommand = HelpCommand;
